package org.summer.generator.writer.impl;

import org.summer.generator.handler.FileCreateHandler;
import org.summer.generator.log.Logger;
import org.summer.generator.log.LoggerManager;
import org.summer.generator.vo.Column;
import org.summer.generator.writer.AbstractFileWriter;
import org.summer.generator.writer.WriterToResource;

/**
 * react antd 表格列文件写入处理类
 */
@WriterToResource(suffix = "TableColumns", extension = FileCreateHandler.REACT_TS_FILE_EXTENSION, toTest = true)
public class TypescriptAntdTableColumnsFileWriter extends AbstractFileWriter {
    private static final Logger log = LoggerManager.getLogger(TypescriptAntdTableColumnsFileWriter.class);
    @Override
    public void execute() {
        channel.writerNewline("import {ColumnsType} from \"antd/es/table\";");
        channel.writerNewline("import {%s} from \"@/type/%s\";".formatted(table.getTableUpperName(),table.getTableUpperName()));
        channel.writerEmptyLine();
        channel.writerNewline("const %sColumns: ColumnsType<%s> = ["
                .formatted(table.getTableLowerName(),table.getTableUpperName()));
        channel.writer("""
                    {
                        key: 'index',
                        dataIndex: 'index',
                        title: '序号',
                        width: 60,
                        align: "center",
                        render: (_:any, __:any, index: number) => <strong> {index + 1} </strong>
                    },
                """);
        table.getColumns().forEach(this::createItem);
        channel.writer("""
                    {
                        key: 'options',
                        dataIndex: 'options',
                        title: '操作',
                        width: 130,
                        align: "center",
                    },
                """);
        channel.writerNewline("];");
        channel.writerEmptyLine();
        channel.writerNewline("export default %sColumns;".formatted(table.getTableLowerName()));
    }

    private void createItem(Column column) {
        channel.writer("""
                    {
                        key: '%s',
                        dataIndex: '%s',
                        title: '%s',
                        width: 120,
                        align: "center",
                    },
                """
                .formatted(column.getColumnName(),column.getColumnName(),column.getRemark()));
    }
}
